//
//  BKStatusListViewModel.swift
//  微博
//
//  Created by mac on 16/8/12.
//  Copyright © 2016年 xbk. All rights reserved.
//

import UIKit

//获取微博数据的viewModel ,对应的视图是首页的tableview
class BKStatusListViewModel: NSObject {

    //微博数据源
    lazy var statusList:[BKStatusViewModel] = [BKStatusViewModel]()
    
    func loadData(callBack:(isSuccess:Bool)->()) {
        
        BKNetworkTools.shareTools.requestStatuses(BKUserAccountViewModel.shareUserAccount.accessToken!) { (response, error) -> () in
            
            if error != nil{
                
                print("网络请求异常,\(error)")
                callBack(isSuccess: false)
                return
            }
            
            guard let dic = response as? [String:AnyObject] else {
                
                print("字典格式不正确")
                callBack(isSuccess: false)
                return
            }
            
            guard let dicArray = dic["statuses"] as? [[String:AnyObject]] else {
                
                print("字典格式不正确")
                callBack(isSuccess: false)
                return
            }
            
            var tempArray = [BKStatusViewModel]()
            for value in dicArray{
                
                let status = BKStatus(dic: value)
                
                let statusViewModel = BKStatusViewModel(status: status)
                
                tempArray.append(statusViewModel)
            }
            
            self.statusList = tempArray
            //self.tableView.reloadData()
            callBack(isSuccess: true)
        }
    }
}
